package android.money;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
//import android.database.ArrayListCursor;
import android.content.Intent;
import android.database.Cursor;
import android.database.MatrixCursor;
import android.money.db.AccountManager;
import android.money.db.DBInfo;
import android.money.db.TransactionManager;
import android.os.Bundle;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class TransactionList extends ListActivity {
	private TransactionManager transactionManager;
	private String[] columNames;
	private long from , to;
	private int accountId;
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		Intent i = getIntent();
		Bundle b = i.getExtras();
		from = b.getLong(ReportOption.FROM_DATE);
		to = b.getLong(ReportOption.TO_DATE);
		accountId = b.getInt(DBInfo.account_id);
		
		transactionManager = new TransactionManager(this);
		transactionManager.open();
		columNames = new String[] { DBInfo.trans_id,"amount_unit", "acc_time" };
		fillData();
	}

	private void fillData() {
		// TODO Auto-generated method stub
		Cursor transactionCusor = transactionManager.fetchTransactions(from,to,accountId);
		MatrixCursor listCursor =  new MatrixCursor(columNames);		
		addRows(transactionCusor,listCursor);
		startManagingCursor(listCursor);
		ListAdapter adapter = new SimpleCursorAdapter(this,
				android.R.layout.simple_list_item_2, listCursor, new String[] {
						"amount_unit", "acc_time" }, new int[] {
						android.R.id.text1, android.R.id.text2 });
		setListAdapter(adapter);
	}

	private void addRows(Cursor tc ,MatrixCursor lc ) {
		AccountManager accManager = new AccountManager(this);
		accManager.open();
		ArrayList<ArrayList> transactions = new ArrayList<ArrayList>();
		while (tc.moveToNext()) {		
			ArrayList transactionList = new ArrayList();
			long id = tc.getLong(tc.getColumnIndexOrThrow(DBInfo.trans_id));
			String amount = ""
					+ tc.getDouble(tc.getColumnIndexOrThrow(DBInfo.trans_amount));
			String unit = tc.getString(tc.getColumnIndexOrThrow(DBInfo.trans_unit));
			String time = ""
					+ new SimpleDateFormat("yyyy/MM/dd - HH:mm:ss").format(new Date(tc.getLong(tc.getColumnIndexOrThrow((DBInfo.trans_time)))));
			//Log.w("TIME in DB", ""+tc.getLong(tc.getColumnIndexOrThrow((DBInfo.trans_time))));
			Cursor accCursor = accManager.fetchAccount(tc.getInt(tc
					.getColumnIndexOrThrow(DBInfo.trans_acc_id)));
			String accName = accCursor.getString(accCursor
					.getColumnIndexOrThrow(DBInfo.account_name));

			transactionList.add(id);
			transactionList.add(amount + " " + unit);
			transactionList.add(accName + "  " + time);
			//Log.w("ADDROW", transactionList.toString());
			transactions.add(transactionList);
		}
		Collections.reverse(transactions);
		for(ArrayList transaction : transactions)
			lc.addRow(transaction);
			//Collections.reverse(transactionList.get(i));
	}
	/*
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		Intent i = new Intent(this, TransactionReport.class);
		i.putExtra(DBInfo.trans_id, id);
		startActivity(i);
	}
    */
}
